<html>
<head>
    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">

function insertSensor(){
  for(var i=0; i<R3_point.features.length; i++){
    soap(i);
  }
}


function soap(i) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', 'http://localhost:8080/52n-sos-webapp/service', true);

                // build SOAP request
    var offeringName = R3_point.features[i].properties.Name;
    var offeringID = R3_point.features[i].properties.id;
    var featuresOfInterest = R3_point.features[i].properties.Name;
    var x = R3_point.features[i].geometry.coordinates[0];
    var y = R3_point.features[i].geometry.coordinates[1];
    var z = R3_point.features[i].geometry.coordinates[2];


    var sensor = 
    '<?xml version="1.0" encoding="UTF-8"?>'+
    '<env:Envelope    xmlns:env="http://www.w3.org/2003/05/soap-envelope"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2003/05/soap-envelope http://www.w3.org/2003/05/soap-envelope/soap-envelope.xsd http://www.opengis.net/sos/2.0 http://schemas.opengis.net/sos/2.0/sosInsertSensor.xsd      http://www.opengis.net/swes/2.0 http://schemas.opengis.net/swes/2.0/swes.xsd">'+
      '<env:Body>'+
        '<swes:InsertSensor            xmlns:swes="http://www.opengis.net/swes/2.0"            xmlns:sos="http://www.opengis.net/sos/2.0"            xmlns:swe="http://www.opengis.net/swe/1.0.1"            xmlns:sml="http://www.opengis.net/sensorML/1.0.1"            xmlns:gml="http://www.opengis.net/gml"            xmlns:xlink="http://www.w3.org/1999/xlink"            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" service="SOS" version="2.0.0">'+
          '<swes:procedureDescriptionFormat>http://www.opengis.net/sensorML/1.0.1</swes:procedureDescriptionFormat>'+
          '<swes:procedureDescription>'+
            '<sml:SensorML version="1.0.1">'+
              '<sml:member>'+
                '<sml:System>'+
                   '<sml:identification>'+
                    '<sml:IdentifierList>'+
                      '<sml:identifier name="uniqueID">'+
                        '<sml:Term definition="urn:ogc:def:identifier:OGC:1.0:uniqueID">'+
                          '<sml:value>DHT22_Sensor</sml:value>'+
                        '</sml:Term>'+
                      '</sml:identifier>'+
                      '<sml:identifier name="longName">'+
                        '<sml:Term definition="urn:ogc:def:identifier:OGC:1.0:longName">'+
                          '<sml:value>DHT22 Sensor in NCU R3 building</sml:value>'+
                        '</sml:Term>'+
                      '</sml:identifier>'+
                      '<sml:identifier name="shortName">'+
                        '<sml:Term definition="urn:ogc:def:identifier:OGC:1.0:shortName">'+
                          '<sml:value>DHT22_Sensor</sml:value>'+
                        '</sml:Term>'+
                      '</sml:identifier>'+
                    '</sml:IdentifierList>'+
                  '</sml:identification>'+
                  '<sml:capabilities name="offerings">'+
                    '<swe:SimpleDataRecord>'+
                      '<swe:field name="' + offeringName +'">'+
                        '<swe:Text definition="urn:ogc:def:identifier:OGC:offeringID">'+
                          '<gml:name>'+ offeringName + '</gml:name>'+
                          '<swe:value>' + offeringID + '</swe:value>'+
                        '</swe:Text>'+
                      '</swe:field>'+
                    '</swe:SimpleDataRecord>'+
                  '</sml:capabilities>'+
                  '<sml:capabilities name="parentProcedures">'+
                    '<swe:SimpleDataRecord>'+
                      '<swe:field name="parentProcedure">'+
                        '<swe:Text>'+
                          '<swe:value>DHT22_Parent</swe:value>'+
                        '</swe:Text>'+
                      '</swe:field>'+
                    '</swe:SimpleDataRecord>'+
                  '</sml:capabilities>'+
                  '<sml:capabilities name="featuresOfInterest">'+
                    '<swe:SimpleDataRecord>'+
                      '<swe:field name="featureOfInterestID">'+
                        '<swe:Text>'+
                          '<swe:value>' + featuresOfInterest +
                          '</swe:value>'+
                        '</swe:Text>'+
                      '</swe:field>'+
                    '</swe:SimpleDataRecord>'+
                  '</sml:capabilities>'+
                  '<sml:position name="sensorPosition">'+
                    '<swe:Position referenceFrame="urn:ogc:def:crs:EPSG::4326">'+
                      '<swe:location>'+
                        '<swe:Vector gml:id="STATION_LOCATION">'+
                          '<swe:coordinate name="westing">'+
                            '<swe:Quantity axisID="x">'+
                              '<swe:uom code="degree"/>'+
                              '<swe:value>' + x + '</swe:value>'+
                            '</swe:Quantity>'+
                          '</swe:coordinate>'+
                          '<swe:coordinate name="northing">'+
                            '<swe:Quantity axisID="y">'+
                              '<swe:uom code="degree"/>'+
                              '<swe:value>' + y + '</swe:value>'+
                            '</swe:Quantity>'+
                          '</swe:coordinate>'+
                          '<swe:coordinate name="altitude">'+
                            '<swe:Quantity axisID="z">'+
                              '<swe:uom code="m"/>'+
                              '<swe:value>1</swe:value>'+
                            '</swe:Quantity>'+
                          '</swe:coordinate>'+
                        '</swe:Vector>'+
                      '</swe:location>'+
                    '</swe:Position>'+
                  '</sml:position>'+
                  '<sml:inputs>'+
                    '<sml:InputList>'+
                      '<sml:input name="observable_property_DHT22">'+
                        '<swe:ObservableProperty definition="observable_property_DHT22"/>'+
                      '</sml:input>'+
                    '</sml:InputList>'+
                  '</sml:inputs>'+
                  '<sml:outputs>'+
                    '<sml:OutputList>'+
                      '<sml:output name="Temperature_DHT22">'+
                        '<swe:Quantity definition="Temperature_DHT22">'+
                          '<swe:uom code="Celsius"/>'+
                        '</swe:Quantity>'+
                      '</sml:output>'+
                      '<sml:output name="Humidity_DHT22">'+
                        '<swe:Quantity definition="Humidity_DHT22">'+
                          '<swe:uom code="Percent"/>'+
                        '</swe:Quantity>'+
                      '</sml:output>'+
                    '</sml:OutputList>'+
                  '</sml:outputs>'+
                '</sml:System>'+
              '</sml:member>'+
            '</sml:SensorML>'+
          '</swes:procedureDescription>'+
          '<swes:observableProperty>Temperature_DHT22</swes:observableProperty>'+
          '<swes:observableProperty>Humidity_DHT22</swes:observableProperty>'+
          '<swes:metadata>'+
            '<sos:SosInsertionMetadata>'+
              '<sos:observationType>http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement</sos:observationType>'+
              '<sos:featureOfInterestType>http://www.opengis.net/def/samplingFeatureType/OGC-OM/2.0/SF_SamplingPoint</sos:featureOfInterestType>'+
            '</sos:SosInsertionMetadata>'+
          '</swes:metadata>'+
        '</swes:InsertSensor>'+
      '</env:Body>'+
    '</env:Envelope>';

console.log(sensor);

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        console.log('done. use firebug/console to see network response');
                    }
                }
            }
            // Send the POST request
            xmlhttp.setRequestHeader('Content-Type', 'application/soap+xml');
            xmlhttp.send(sensor);
            // send request
            // ...
        }
    </script>
</head>
<body>
    <form name="Demo" action="" method="post">
        <div>
            <input type="button" value="Soap" onclick="insertSensor();" />
        </div>
    </form>
</body>
  <script type="text/javascript" src="../../GeoJSON/R3_point_GeoJSON.js"></script> 
</html> <!-- typo -->